/*
 * SvrClient.h
 *
 *  Created on: 2012-7-11
 *      Author: ThinkPad
 */

#ifndef SVRCLIENT_H_
#define SVRCLIENT_H_

#include "SvrConfig.h"
#include "SvrSession.h"
#include "user_token.h"
#include "http.h"
#include "SvrTask.h"
#include "Svr_log.h"
#include "SvrFileManage.h"
#include "DataClip.h"
/*
*op buf len
*/
#define op_buf_len 0xffff
/*
*op_buf
*/
struct op_buf
{
    ubyte * buf;
    size_t len;

    op_buf()
    {
        buf = NULL;
        len = 0;
    }
};


/*
*svr client connect type
*/
enum Client_Type
{
    INVALID_TYPE,
    /*go on receive*/
    ON_RECV_TYPE,
    /*browse http reuqest*/
    HTTP_BROWSE,
    /*vod htb Node*/
    HTTP_HTB_CLIENT,
    /*http admin client*/
    HTTP_ADMIN_CLIENT,
    /*m3u8 client*/
	HTTP_M3U8_CLIENT,
	/*m3u8 ts client*/
	HTTP_M3U8TS_CLIENT,
    /*player client*/
    PLAYER_CLIENT,
    /*download Node*/
    DOWNLOAD_CLIENT,
    /*live stream client*/
	LIVE_STREMA_CLIENT,
	/*live timeback client*/
	LIVE_TIMEBACK_CLIENT,
	/*live timeinfo client*/
	LIVE_TIMEINFO_CLIENT,
	/*live rerun client*/
	LIVE_RERUN_CLIENT,
    /*CDN autheration client*/
    CDN_AUTH_CLIENT,
    /*svr heartbeat*/
    SVR_HEARTBEAT,
	/*capture udp ts stream*/
	CAPTURE_UDPTS,
	/*capture http stream*/
	CAPTURE_HTTP_STREAM,
	/*CDN live Client*/
	CDN_LIVE_CLIENT,
	/*CDN live server*/
	CDN_LIVE_SVR
};


/**
 * SvrClient utils class
 */
class SvrClient
{
public:
        SvrClient(void);
        ~SvrClient(void);

public:
        /*
        *SvrClient init
        */
        int  init(SvrConfig * pconfig,SvrTask * psvrtask,Svr_log * psvrlog,SvrFileManage * psvrfilemanage,int init_size = 100);

        /*
        *client token resource
        */
        void alloc(user_token ** client);
        void free(user_token * client);
        int  get_free_count();
        int  get_count();

        /*
        *http request handler
        */
        Client_Type anylsis_client_type(char * buf,size_t buf_len,user_token * token);

        /*
        *session count
        */
        int get_session_count();
        int get_free_session_count();

        /*
        * http user session
        */
        AUTH_STATUS get_client_session_auth_status(const char * key);
        bool set_client_session_auth_status(const char * key,AUTH_STATUS status);

        /*
        *close_client_session,flag:0,FALSE,set session close,1,TRUE,remove session
        *trans_size:last trans data size
        */
        int close_client_session(char * key,int flag ,uint64_t trans_size = 0);

        /*
        *clear_time_out_client_session
        *return clear count
        */
        int clear_time_out_client_session(void);

        /**
         * get_next_session_auth_task
         */
        user_token * get_next_session_auth_task(void);

        /**
         * struct_auth_request_data
         */
        uint32_t struct_auth_request_data(char * buf,size_t buf_len,const char * key);

        /**
         * anylsis_auth_result
         */
        AUTH_STATUS anylsis_auth_result(char * buf,size_t buf_len,const char * key);

        /*
		*force client list offline
		*key,key,key...
		*/
		int force_client_list_offline(const char * keys);

		/*
		*clear_client_connect_by_request_content
		*/
		int clear_client_connect_by_request_content(const char * fileName);

		/**
		 * struct live data request header
		 */
		uint32_t struct_stream_request_data(char * buf,size_t buf_len,uint16_t id,uint16_t fmt,const char * stream_name = NULL,uint64_t index = 0);

		/*
		 *struct server heartbeat data
		 */
		uint32_t struct_heartbeat_data(char * buf,size_t buf_len,int cur_session_count,int cur_connection_count);

		/*
         * run function
         * tick value: useconds
         * */
        void run(long useconds);

protected:
        /*
        *http request header buf
        */
        void alloc_request_header(http_request_header ** hrt);
        void free_request_header(http_request_header * hrt);

        /*
        *op buf alloc and free
        */
        void alloc_op_buf(op_buf ** pbf);
        void free_op_buf(op_buf * pbf);


        /*add session*/
		int add_client_session(http_request_header * hrt,user_token * token);

		/*force online*/
		int force_client_session_offline(const char * key);

		/*
		*get count by session request content name
		*/
		int get_count_by_request_content(const char * fileName);

        /*
        *open reqeust file
        */
        bool open_request_file(http_request_header * hrt,user_token * token);
        /*
        *decode file name
        */
        bool decode_file_name(http_request_header * hrt,user_token * token);

        /*
        *decode pars
        */
        bool decode_pars(http_request_header * hrt,user_token * token);

        /*
        *decode private key
        */
        bool decode_private_key(http_request_header * hrt);

        /*
         * encode private key
         */
        bool encode_private_key(char * data, size_t data_len,size_t max_buf_len);

        /**
		 * set request content offset
		 */
		void set_request_content_offset(http_request_header * hrt,user_token * token);

		 /*
		*administrator operation cmd
		*/
		Client_Type  administrator_op_cmd(http_request_header *hrt);

		/**
		 * anylsis_m3u8ts
		 */
		Client_Type anylsis_m3u8ts(http_request_header * hrt,user_token * token);
private:
        /*
        *use token
        */
        stack<user_token *> client_pool;
        smart_lock pool_lock;
        int count;

        /*
        *client session manage
        */
        SvrSession  sessionMgr;
        uint32_t connid;/*conn id*/

        /*
        *http request data
        */
        stack<http_request_header *> http_request_header_pool;
        smart_lock http_lock;

        /*
        *op tempt buf
        */
        stack<op_buf * > op_buf_pool;
        smart_lock op_buf_lock;

        /*
	    *auth list queue
	    */
	    queue<user_token *> handle_auth_list;
	    smart_lock handle_lock;

        /*
        *svr config
        */
        SvrConfig  * pSvrconfig;
        SvrTask    * pSvrtask;
        Svr_log    * pSvrlog;
        SvrFileManage * pSvrFileUtils;

        /*time tick*/
        int        SessionTick;
};

#endif /* SVRCLIENT_H_ */
